package com.office.web.controller.chat;

import com.office.common.annotation.Log;
import com.office.common.core.controller.BaseController;
import com.office.common.core.domain.AjaxResult;
import com.office.common.enums.BusinessType;
import com.office.framework.websocket.ChatMessage;
import com.office.framework.websocket.ChatUserInfo;
import com.office.framework.websocket.ChatWebSocketHandler;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 群聊控制器
 *
 * @author 数字牧马人
 */
@Slf4j
@RestController
@RequestMapping("/chat")
@Tag(name = "群聊管理")
public class ChatController extends BaseController {

    @Autowired
    private ChatWebSocketHandler chatWebSocketHandler;

    /**
     * 获取在线用户数量
     */
    @GetMapping("/online/count")
    @Operation(summary = "获取在线用户数量")
    public AjaxResult getOnlineUserCount() {
        int count = chatWebSocketHandler.getOnlineUserCount();
        return AjaxResult.success("获取成功", count);
    }

    /**
     * 获取在线用户列表
     */
    @GetMapping("/online/users")
    @Operation(summary = "获取在线用户列表")
    public AjaxResult getOnlineUsers() {
        Map<String, ChatUserInfo> users = chatWebSocketHandler.getOnlineUsers();
        return AjaxResult.success("获取成功", users);
    }

    /**
     * 发送系统消息
     */
    @PostMapping("/system/message")
    @Log(title = "发送系统消息", businessType = BusinessType.INSERT)
    @Operation(summary = "发送系统消息")
    public AjaxResult sendSystemMessage(@RequestBody Map<String, String> params) {
        String content = params.get("content");
        if (content == null || content.trim().isEmpty()) {
            return AjaxResult.error("消息内容不能为空");
        }

        ChatMessage message = new ChatMessage();
        message.setType("system");
        message.setContent(content);
        message.setUsername("系统管理员");

        // 通过WebSocket处理器广播系统消息
        chatWebSocketHandler.broadcastMessage(message);

        log.info("发送系统消息: {}", content);

        return AjaxResult.success("系统消息发送成功");
    }

    /**
     * 获取聊天统计信息
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取聊天统计信息")
    public AjaxResult getChatStatistics() {
        Map<String, Object> statistics = new HashMap<>();
        statistics.put("onlineUserCount", chatWebSocketHandler.getOnlineUserCount());
        statistics.put("onlineUsers", chatWebSocketHandler.getOnlineUsers());
        statistics.put("serverTime", System.currentTimeMillis());

        return AjaxResult.success("获取成功", statistics);
    }

    /**
     * 清空聊天记录（仅清空服务器端缓存）
     */
    @DeleteMapping("/clear")
    @Log(title = "清空聊天记录", businessType = BusinessType.DELETE)
    @Operation(summary = "清空聊天记录")
    public AjaxResult clearChatHistory() {
        // 发送系统消息通知所有用户聊天记录已清空
        ChatMessage clearMessage = new ChatMessage();
        clearMessage.setType("system");
        clearMessage.setContent("聊天记录已清空");
        clearMessage.setUsername("系统管理员");

        chatWebSocketHandler.broadcastMessage(clearMessage);

        log.info("清空聊天记录");
        return AjaxResult.success("聊天记录已清空");
    }

    /**
     * 获取AI助手信息
     */
    @GetMapping("/ai/info")
    @Operation(summary = "获取AI助手信息")
    public AjaxResult getAIInfo() {
        Map<String, Object> aiInfo = new HashMap<>();
        aiInfo.put("model", "deepseek-r1:1.5b");
        aiInfo.put("description", "专业的办公助手，可以回答问题、提供建议");
        aiInfo.put("triggers", new String[]{"ai", "助手", "帮助", "请问", "怎么", "如何", "为什么"});

        return AjaxResult.success("获取成功", aiInfo);
    }

    /**
     * WebSocket连接测试端点
     */
    @GetMapping("/ws/test")
    @Operation(summary = "WebSocket连接测试")
    public AjaxResult testWebSocket() {
        return AjaxResult.success("WebSocket端点可访问");
    }
}
